using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using CloudStorage.API;
using CloudStorage.Factories;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CloudStorage.Tests.API.Extensibility.API
{
	[TestClass]
	public class StorageAccountInfoExtensibilityTests
	{
		IStorageAccountInfoFactory StorageAccountInfoFactory;

		[TestInitialize]
		public void Create_MEF_catalog_and_container()
		{
			var catalog = new TypeCatalog(typeof(StorageAccountInfoMock), typeof(TrivialStorageAccountInfoFactory));

			var container = new CompositionContainer(catalog);

			StorageAccountInfoFactory = container.GetExportedValue<IStorageAccountInfoFactory>();

			Assert.IsNotNull(StorageAccountInfoFactory);
		}

		[TestMethod]
		public void Getting_StorageAccountInfo_by_metadata_using_MEF()
		{
			string storageAccountName = "foobar account";

			IStorageAccountInfo storageAccountInfo = StorageAccountInfoFactory.Get(storageAccountName);

			Assert.IsNotNull(storageAccountInfo);
		}

		[TestMethod]
		public void Cannot_get_StorageAccountInfo_with_wrong_name()
		{
			string storageAccountName = "some other account that does not exist";

			IStorageAccountInfo storageAccountInfo = StorageAccountInfoFactory.Get(storageAccountName);

			Assert.IsNull(storageAccountInfo);
		}

		// Export attribute here ensures we can get the IStorageAccountInfoFactory using MEF
		[Export(typeof(IStorageAccountInfoFactory))]
		public class TrivialStorageAccountInfoFactory : IStorageAccountInfoFactory
		{
			[Import]
			public Lazy<IStorageAccountInfo, IStorageAccountInfoMetadata> StorageAccountInfo { get; set; }

			public IStorageAccountInfo Get(string storageAccountName)
			{
				if (storageAccountName == StorageAccountInfo.Metadata.Name)
				{
					return StorageAccountInfo.Value;
				}
				return null;
			}
		}

		// Custom StorageAccountInfoMetadataAttribute here exports IStorageAccountInfo implementation including metadata using MEF.
		[StorageAccountInfoMetadata("foobar account")]
		public class StorageAccountInfoMock : IStorageAccountInfo
		{
			public string Name
			{
				get { throw new NotImplementedException(); }
			}

			public string Base64Key
			{
				get { throw new NotImplementedException(); }
			}

			public Uri BaseAddress
			{
				get { throw new NotImplementedException(); }
			}
		}
	}
}